// LoadGameDialog.xaml.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using Supremacy.Client.Commands;
using Supremacy.Game;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for LoadGameDialog.xaml
    /// </summary>
    public partial class LoadGameDialog
    {
        #region Constructors
        public LoadGameDialog()
        {
            InitializeComponent();

            IsVisibleChanged += LoadGameDialogIsVisibleChanged;
            SaveGameList.SelectionChanged += SaveGameListSelectionChanged;

            InputBindings.Add(
                new KeyBinding(GenericCommands.CancelCommand,
                               Key.Escape,
                               ModifierKeys.None));
            InputBindings.Add(
                new KeyBinding(GenericCommands.AcceptCommand,
                               Key.Enter,
                               ModifierKeys.None));

            CommandBindings.Add(
                new CommandBinding(GenericCommands.CancelCommand,
                                   GenericCommandsCancelCommandExecuted));
            CommandBindings.Add(
                new CommandBinding(GenericCommands.AcceptCommand,
                                   GenericCommandsAcceptCommandExecuted));
        }
        #endregion

        #region Methods
        private void GenericCommandsAcceptCommandExecuted(object source, ExecutedRoutedEventArgs e)
        {
            var saveGameHeader = SaveGameList.SelectedItem as SavedGameHeader;
            if (saveGameHeader == null)
                return;
            Close();
            ClientCommands.LoadGame.Execute(saveGameHeader);
        }

        private void GenericCommandsCancelCommandExecuted(object source, ExecutedRoutedEventArgs e)
        {
            Close();
        }

        private void LoadGameDialogIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!IsVisible)
                return;
            this.DataContext = SavedGameManager.FindSavedGames();
            Focus();
            SaveGameList.Focus();
        }

        private void SaveGameListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SaveGameInfoText.Visibility = (SaveGameList.SelectedItem is SavedGameHeader)
                ? Visibility.Visible
                : Visibility.Hidden;
        }
        #endregion
    }
}